package com.wudl.queue.controller;

import com.wudl.queue.domain.User;
import com.wudl.queue.service.UserService;
import com.wudl.queue.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：wudl
 * @date ：Created in 2021-12-12 20:49
 * @description：控制层
 * @modified By：
 * @version: 1.0
 */

@RestController
public class UserController {

//    @Autowired
//    Producer  producer;

    private static String lock = "";
    @Autowired
    UserService userService;
    int i = 0;

//    @GetMapping("/user/add")
//    public String addUser() throws Exception {
//
//
////        producer.offer("11111111111111111111111");
//
//
//            i++;
//
//            User u = new User();
//            u.setId(i);
//            u.setName("flink" + i);
//            userService.sendQueue(u);
//        FileWriter fw = new FileWriter("D:\\tmp\\2shengchan.txt", true);
//        BufferedWriter bw = new BufferedWriter(fw);
//        bw.append("生产");
//        bw.write(u.toString()+"\r\n ");// 往已有的文件上添加字符串
//
//        bw.close();
//        fw.close();
//        System.out.println("---"+u.toString());
//
//        return "hellowrold";
//
//
//    }

    @GetMapping("/user/findall")
    public List<User> findall() throws Exception {


        List<User> all = userService.findAll();

        return all;


    }

    @GetMapping("/user/addUser")
    public String addUser() throws Exception {

        User u = new User();
        u.setId(9);
        u.setUserNo("100014");
        u.setUserName("myhdfs");

        userService.addUser(u);

        return "success";


    }

    @GetMapping("/user/insertBatch")
    public String insertBatch() throws Exception {
        i++;

        User u = new User();
        u.setId(9);
        u.setUserNo("100014"+i);
        u.setUserName("myFlink"+i);
        userService.insertBatchTow(u);
            FileWriter fw = new FileWriter("D:\\tmp\\x.txt", true);
            BufferedWriter bw = new BufferedWriter(fw);

            bw.append("消费-->"+i);
            bw.write(u.toString() + "\r\n ");
            bw.close();
            fw.close();
        return "success";


    }
}
